package com.oracle.lingshangejia.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.activity.AddEvaluateActivity;
import com.oracle.lingshangejia.activity.ResidentDetailActivity;
import com.oracle.lingshangejia.activity.ServicePackageActivity;
import com.oracle.lingshangejia.activity.SpecialManagementActivity;
import com.oracle.lingshangejia.activity.TreatPatientActivity;
import com.oracle.lingshangejia.activity.VisitRecordActivity;
import com.oracle.lingshangejia.bean.ResidentBean;
import com.oracle.lingshangejia.bean.SignBean;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.HashMap;
import java.util.List;
//
public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.VH> {

    private Context context;
    private List<ResidentBean.DataBean> data;
    private OnRefreshLinsner linsner;
    public interface OnRefreshLinsner{
        void refresh();
    }

    public void addDate(List<ResidentBean.DataBean> list){
        data = list;
        notifyDataSetChanged();

    }

    //加载更多
    public void loadmoreDate(List<ResidentBean.DataBean> list){
        data.addAll(list);
        notifyDataSetChanged();

    }
    private OnItemClickListener onItemClickListener;
    public interface OnItemClickListener{
        void onItemClick(View view, int postion);
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public void setLinsner(OnRefreshLinsner linsner) {
        this.linsner = linsner;
    }

    public FileListAdapter(Context context, List<ResidentBean.DataBean> data) {
        this.context=context;
        this.data=data;
    }
    @Override
    public void onBindViewHolder(@NonNull VH vh, int i) {
        vh.tv_residentfile_name.setText(data.get(i).getXm());
        String sex;
        if(data.get(i).getXb()==2){
            sex="女";
            vh.tv_sex_age.setText("女");
        }else{
            sex="男";
            vh.tv_sex_age.setText("男");
        }
        if(data.get(i).getSfqy()==0){
            vh.tv_residentfile_status.setText("未签约");
            vh.tv_residentfile_qy.setTextColor(context.getResources().getColor(R.color.black));
            vh.tv_residentfile_qy.setClickable(true);

        }else{
            vh.tv_residentfile_status.setText("已签约");
            vh.tv_residentfile_qy.setTextColor(context.getResources().getColor(R.color.image_overlay_true));
            vh.tv_residentfile_qy.setClickable(false);

        }

        //签约
        vh.tv_residentfile_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                   if(data.get(i).getSfqy()==0){
                    showIdCardSignDialog(data.get(i).getSfzh(),data.get(i).getXm(),sex,data.get(i).getId());
                }
            }
        });
        //名字
        vh.tv_residentfile_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent = new Intent(context, ResidentDetailActivity.class);
               intent.putExtra("jmid", data.get(i).getId());//居民id
               context.startActivity(intent);
            }
        });
        vh.tv_residentfile_jz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //context.startActivity(new Intent(context,ConsultationActivity.class));
                Intent intent=new Intent(context, TreatPatientActivity.class);
                intent.putExtra("jmid",data.get(i).getId());
                context.startActivity(intent);
            }
        });
        vh.rl_sf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, VisitRecordActivity.class);
                intent.putExtra("id",data.get(i).getId());
                context.startActivity(intent);
            }
        });

        vh.rl_checkhealth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,ServicePackageActivity.class);
                intent.putExtra("id", data.get(i).getId());
                context.startActivity(intent);
                //体检记录
                /*Intent intent=new Intent(context,CheckupHealthActivity.class);
                intent.putExtra("tjbh",data.get(i).getBh());
                intent.putExtra("jmid",data.get(i).getId());
                context.startActivity(intent);*/
            }
        });
        vh.tv_tjbg.setText("出生日期: "+data.get(i).getCsrq().substring(0, 11));
        vh.tv_tjbg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //体检记录
               /* Intent intent=new Intent(context,CheckupHealthActivity.class);
                intent.putExtra("tjbh",data.get(i).getBh());
                intent.putExtra("jmid",data.get(i).getId());
                context.startActivity(intent);*/
            }
        });
        vh.rl_see_specialitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //专项
                Intent intent=new Intent(context, SpecialManagementActivity.class);
                intent.putExtra("jmid",data.get(i).getId());
                intent.putExtra("tjbh",data.get(i).getBh());
                context.startActivity(intent);
            }
        });
        RequestOptions options = new RequestOptions().circleCrop()
                .placeholder(R.drawable.ic_info_user)
                .error(R.drawable.ic_info_user)
                .fallback(R.drawable.ic_info_user);
             //   .centerCrop()

        Glide.with(context)
                .load(Constant.imgurl+data.get(i).getTxUrl())
                .apply(options)
                .into(vh.iv_residentfile_img);
        vh.tv_residentfile_qy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //评估
                Intent intent = new Intent(context,AddEvaluateActivity.class);
                intent.putExtra("id", data.get(i).getId());
                context.startActivity(intent);
              /*  if(data.get(i).getSfqy()==0){
                    showIdCardSignDialog(data.get(i).getSfzh(),data.get(i).getXm(),sex,data.get(i).getId());
                }*/
            }
        });
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(v,i);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return data!=null?data.size():0;
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        //LayoutInflater.from指定写法
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.residentfile_item, parent, false);
        return new VH(v);
    }
    //② 创建ViewHolder
    public static class VH extends RecyclerView.ViewHolder{
        public final TextView tv_residentfile_name;  //姓名
        TextView tv_sex_age;  //年龄  男-50周岁
        TextView tv_residentfile_status;  //签约未签约
        TextView tv_bz;  //备注
        ImageView iv_residentfile_img;
        TextView tv_residentfile_qy; //签约按钮
        View view_residentfile;  //竖条
        TextView tv_residentfile_jz;//接诊
        RelativeLayout rl_checkhealth;//体检记录
        RelativeLayout rl_see_specialitem;//查看专项
        RelativeLayout rl_sf;//随访
        TextView tv_tjbg;
        public VH(View v) {
            super(v);
            tv_tjbg = v.findViewById(R.id.tv_csrq);
            tv_residentfile_name = (TextView) v.findViewById(R.id.tv_residentfile_name);
            tv_sex_age=v.findViewById(R.id.tv_sex_age);
            tv_residentfile_status=v.findViewById(R.id.tv_residentfile_status);
            tv_bz=v.findViewById(R.id.tv_bz);
            iv_residentfile_img=v.findViewById(R.id.iv_residentfile_img);
            tv_residentfile_qy=v.findViewById(R.id.tv_residentfile_qy);
            view_residentfile=v.findViewById(R.id.view_residentfile);
            tv_residentfile_jz=v.findViewById(R.id.tv_residentfile_jz);
            rl_checkhealth=v.findViewById(R.id.rl_checkhealth);
            rl_see_specialitem=v.findViewById(R.id.rl_see_specialitem);
            rl_sf=v.findViewById(R.id.rl_sf);
        }
    }
    /**
     * 弹出签约对话框
     */
    Dialog dialog;
    private void showIdCardSignDialog(final String number, String name, String sex, final int id){
        dialog= new Dialog(context, R.style.dialog);
        View view_dialog = LayoutInflater.from(context).inflate(
                R.layout.idcardsign_dialog, null);
        TextView tv_sign_name=view_dialog.findViewById(R.id.tv_sign_name);
        TextView tv_sign_sex=view_dialog.findViewById(R.id.tv_sign_sex);
        TextView tv_sign_number=view_dialog.findViewById(R.id.tv_sign_number);
        TextView tv_sign_cancle=view_dialog.findViewById(R.id.tv_sign_cancle);
        TextView tv_sign_ok=view_dialog.findViewById(R.id.tv_sign_ok);
        tv_sign_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tv_sign_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //签约接口
                int ysid= (int) SpUtils.get(context,"ysid",-1);
                getNetToSign(id,ysid);
            }
        });
        tv_sign_name.setText("姓名："+name);
        tv_sign_sex.setText("性别："+sex);
        tv_sign_number.setText("身份证号："+number);
        dialog.setContentView(view_dialog);
        Window dialogWindow = dialog.getWindow();
        //设置位置
        dialogWindow.setGravity(Gravity.CENTER);
        //设置dialog的宽高属性
        dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
    }
    private  void getNetToSign(int id,int ysid){
        HashMap<Object,Object> map = new HashMap<>();
        map.put("jmId",id);
        map.put("ysId",ysid);
        OkHttpUtils.getNetWork(context, Constant.baseurl+"online/signing",map,signhandler);
    }
    private Handler signhandler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    SignBean bean=gson.fromJson(s, SignBean.class);
                    if(bean.getCode()==0){  //签约成功
                        Toast.makeText(context,bean.getMsg(),Toast.LENGTH_SHORT).show();
                        if(dialog!=null){
                            dialog.dismiss();
                        }
                        if(linsner!=null){
                            linsner.refresh();
                        }
                        //刷新页面
                    }else{
                        Toast.makeText(context,bean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(context,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(context,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
